package com.example.study.controller;

import com.example.study.api.UserRemoteClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @description：
 * @author: liqiancheng
 * @createTime: 2021/8/19 11:05
 **/
@RestController
public class HystrixController {

    @Autowired
    private RestTemplate restTemplate;

    @Resource
    private UserRemoteClient userRemoteClient;

    @HystrixCommand(fallbackMethod = "defaultCallHello",commandProperties = {
            @HystrixProperty(name="execution.isolation.strategy", value = "THREAD")
    }
    )
    @GetMapping("/callHello")
    public String callHello() {
        String result = restTemplate.getForObject("http://localhost:8081/user/hello", String.class);
        return result;
    }

    public String defaultCallHello() {
        return "fail";
    }

    @GetMapping("/hystrix/hello")
    public String hystrixHello() {
        return userRemoteClient.hello();
    }
}
